import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import routes from './router.js'
import element from "element-ui"
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/theme-chalk/display.css'
import './assets/style.css'
import fHeader from './components/f-header.vue'
import fFooter from './components/f-footer.vue'
import i18n from './i18n/i18n.js'
import VueResource from 'vue-resource'
import VueCookies from 'vue-cookies'
import Vuex from 'vuex'
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'


/* import Highlight from './highlight.js';
 
Vue.use(Highlight); */

Vue.use(element);

Vue.use(mavonEditor)

Vue.use(VueResource)

Vue.use(VueCookies)
Vue.use(Vuex)

Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.component("f-header",fHeader)
Vue.component("f-footer",fFooter)

//Vue.use(ElementUI, {
//i18n: (key, value) => i18n.t(key, value)
//})

const router =new VueRouter({
	routes,
	base: '/',
	scrollBehavior(to,from,savedPosition){
        return {x:0,y:0}
      }
})

const store = new Vuex.Store({
    state:{
		isLogin: false,
		user:""
	},
    mutations:{
		userStatus(state, flag) {
			state.isLogin = flag
		},
		setuser(state,flag){
			state.user = flag;
			window.$cookies.set("user_session","haslogin",60 * 60 * 24)
		},
		getuser(state,flag){
		   
		 var value =  window.$cookies.get("user_session")


			if(value === "haslogin"){

				state.isLogin = true;
			}
	
		}
	
	},
    actions:{
		userLogin({commit}, flag) {
			commit("userStatus", flag)
		},
		setusername ({commit},flag){
			commit("setuser",flag)
		},
		getusername ({commit},flag){
			commit("getuser",flag)
		}
	},
    getters:{
		isLogin: state => state.isLogin,
		user:state =>state.user
	}
})



router.beforeEach((to, from, next) => {


	//获取用户登录成功后储存的登录标志
	store.dispatch("getusername");


  /* for (var i in getFlag) { // 如果不为空，则会执行到这一步，返回true
    sum = true
} */
  // 如果为空,返回false


	//如果登录标志存在且为isLogin，即用户已登录
	if( store.getters.isLogin ){

	  //设置vuex登录状态为已登录
	  next()
  
	}else{
  
	  //用户想进入需要登录的页面，则定向回登录界面 
		if(to.meta.requireAuth){
		next({
		  path: '/login',
		})
		//iViewUi友好提示
		alert('请先登录')
	  //用户进入无需登录的界面，则跳转继续
	  }else{
		next()
	  }
  
	}
  
  });

new Vue({
	store,
	router,
	i18n,
	render: h => h(App),
	
}).$mount('#app')




